// 引入axios
import axios from 'axios'

// 设置基准路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'

// 添加请求拦截器  ---- axios官网
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // 获取本地存储里面的token
  var token = localStorage.getItem('itcast_token')
  // 判断token是否存在
  if (token) {
    // 将token添加到请求头里面
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 发送请求  验证登录信息
export const login = (formInfo) => {
  return axios.post('login', formInfo)
    .then((result) => {
      return result.data
    })
}

// 获取用户数据 -- 渲染列表
export const GetUserList = (formInfo) => {
  return axios.get('users', {params: formInfo})
    .then((result) => {
      return result.data
    })
}

// 实现添加用户
export const addUser = (pa) => {
  return axios.post('users', pa)
    .then(res => {
      return res.data
    })
}

// 实现用户编辑
export const editUser = (pa) => {
  return axios.put(`users/${pa.id}`, pa)
    .then(res => {
      return res.data
    })
}

// 实现删除单个用户
export const deleteUser = (id) => {
  return axios.delete(`users/${id}`)
    .then(res => {
      return res.data
    })
}

// 实现角色列表的查询
export const grentList = () => {
  return axios.get('roles')
    .then(res => {
      return res.data
    })
}

// 实现分配用户角色
export const grantUser = (pa) => {
  return axios.put(`users/${pa.id}/role`, {rid: pa.rid})
    .then(res => {
      return res.data
    })
}

// 实现用户状态修改
export const changeState = (uId, type) => {
  return axios.put(`users/${uId}/state/${type}`)
    .then(res => {
      return res.data
    })
}

// 根据id查询用户信息
export const getUserById = (id) => {
  return axios.get(`users/${id}`)
    .then(res => {
      return res.data
    })
}

// 获取所有权限列表
export const getRightList = (type) => {
  return axios.get(`rights/${type}`)
    .then(res => {
      return res.data
    })
}

// 添加角色
export const addRole = (pa) => {
  return axios.post('roles', pa)
    .then(res => {
      return res.data
    })
}

// 编辑角色信息后提交
export const editRole = (pa) => {
  return axios.put(`roles/${pa.id}`, pa)
    .then(res => {
      return res.data
    })
}

// 删除角色
export const deleteRole = (id) => {
  return axios.delete(`roles/${id}`)
    .then(res => {
      return res.data
    })
}

// 角色授权
export const grantRole = (roleId) => {
  return axios.post(`roles/${roleId}/rights`)
    .then(res => {
      return res.data
    })
}

// 单个角色权限的修改-赋值
export const grantRightForRole = (roleId, rids) => {
  return axios.post(`roles/${roleId}/rights`, {rids: rids})
    .then(res => {
      return res.data
    })
}

// 角色权限删除
export const deleteRightByRoleId = (roleId, rightId) => {
  return axios.delete(`roles/${roleId}/rights/${rightId}`)
    .then(res => {
      return res.data
    })
}
// 左侧菜单权限
export const leftRightMenus = () => {
  return axios.get('menus')
    .then(res => {
      return res.data
    })
}

// 左侧菜单权限
export const getGoodsList = (pa) => {
  return axios.get('goods', {params: pa})
    .then(res => {
      return res.data
    })
}

// 获取商品分类信息
export const getCategoriesList = (pa) => {
  // return axios.get('categories', {params: {'type': type}})
  return axios.get('categories', pa)
    .then(res => {
      return res.data
    })
}

// 添加商品
export const addGoods = (pa) => {
  return axios.post('goods', pa)
    .then(res => {
      return res.data
    })
}

// 提交商品信息的编辑结果
export const putGoodsInfo = (pa) => {
  return axios.put(`categories/${pa.id}`, pa)
    .then(res => {
      return res.data
    })
}

// 添加商品分类
export const addCategories = (pa) => {
  return axios.post('categories', pa)
    .then(res => {
      return res.data
    })
}

// 删除商品分类
export const deleteCategories = (id) => {
  return axios.delete(`categories/${id}`)
    .then(res => {
      return res.data
    })
}
